﻿Partial Public Class WpfMessageBox

    Shared mWpfMessageBoxResult As WpfMessageBoxResult
    Dim mWpfMessageBoxButton As WpfMessageBoxButton
    Shared mSkin As DisplaySkin
    Public Shared Property Skin() As DisplaySkin
        Get
            Return mSkin
        End Get
        Set(ByVal value As DisplaySkin)
            mSkin = value
        End Set
    End Property

    Public Overloads Shared Function Show(ByVal text As String) As WpfMessageBoxResult
        Dim msg As New WpfMessageBox()
        msg.MessageTitle.Text = String.Empty
        msg.MessageBlock.Text = text
        msg.SetVisibility(WpfMessageBoxButton.OK)
        msg.SetImage(WpfMessageBoxImage.None)
        msg.ShowDialog()
        Return mWpfMessageBoxResult
    End Function

    Public Overloads Shared Function Show(ByVal text As String, ByVal caption As String) As WpfMessageBoxResult
        Dim msg As New WpfMessageBox()
        msg.MessageTitle.Text = caption
        msg.MessageBlock.Text = text
        msg.SetVisibility(WpfMessageBoxButton.OK)
        msg.SetImage(WpfMessageBoxImage.None)
        msg.ShowDialog()
        Return mWpfMessageBoxResult
    End Function

    Public Overloads Shared Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton) As WpfMessageBoxResult
        Dim msg As New WpfMessageBox()
        msg.MessageTitle.Text = caption
        msg.MessageBlock.Text = text
        msg.SetVisibility(button)
        msg.SetImage(WpfMessageBoxImage.None)
        msg.ShowDialog()
        Return mWpfMessageBoxResult
    End Function

    Public Overloads Shared Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton, ByVal icon As WpfMessageBoxImage) As WpfMessageBoxResult
        Dim msg As New WpfMessageBox()
        msg.MessageTitle.Text = caption
        msg.MessageBlock.Text = text
        msg.SetVisibility(button)
        msg.SetImage(icon)
        msg.ShowDialog()
        Return mWpfMessageBoxResult
    End Function

    Public Overloads Shared Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton, ByVal icon As WpfMessageBoxImage, ByVal defaultResult As WpfMessageBoxResult) As WpfMessageBoxResult
        Dim msg As New WpfMessageBox()
        msg.MessageTitle.Text = caption
        msg.MessageBlock.Text = text
        msg.SetVisibility(button)
        msg.SetImage(icon)
        msg.SetDefaultResult(defaultResult)
        msg.ShowDialog()
        Return mWpfMessageBoxResult
    End Function


    Private Sub SetVisibility(ByVal button As WpfMessageBoxButton)
        mWpfMessageBoxButton = button
        If button = WpfMessageBoxButton.OK Then
            btnOk.IsDefault = True
            btnCancel.Visibility = Windows.Visibility.Collapsed
            btnYes.Visibility = Windows.Visibility.Collapsed
            btnNo.Visibility = Windows.Visibility.Collapsed
            btnOk.Visibility = Windows.Visibility.Visible
        ElseIf button = WpfMessageBoxButton.OKCancel Then
            btnOk.IsDefault = True
            btnCancel.Visibility = Windows.Visibility.Visible
            btnYes.Visibility = Windows.Visibility.Collapsed
            btnNo.Visibility = Windows.Visibility.Collapsed
            btnOk.Visibility = Windows.Visibility.Visible
        ElseIf button = WpfMessageBoxButton.YesNo Then
            btnYes.IsDefault = True
            btnCancel.Visibility = Windows.Visibility.Collapsed
            btnYes.Visibility = Windows.Visibility.Visible
            btnNo.Visibility = Windows.Visibility.Visible
            btnOk.Visibility = Windows.Visibility.Collapsed
        ElseIf button = WpfMessageBoxButton.YesNoCancel Then
            btnYes.IsDefault = True
            btnCancel.Visibility = Windows.Visibility.Visible
            btnYes.Visibility = Windows.Visibility.Visible
            btnNo.Visibility = Windows.Visibility.Visible
            btnOk.Visibility = Windows.Visibility.Collapsed
        End If
    End Sub

    Private Sub SetDefaultResult(ByVal defaultResult As WpfMessageBoxResult)
        If defaultResult = WpfMessageBoxResult.Yes Then
            btnYes.IsDefault = True
            btnYes.Focus()
            btnNo.IsDefault = False
            btnCancel.IsDefault = False
            btnOk.IsDefault = False
        ElseIf defaultResult = WpfMessageBoxResult.No Then
            btnNo.IsDefault = True
            btnNo.Focus()
            btnYes.IsDefault = False
            btnCancel.IsDefault = False
            btnOk.IsDefault = False
        ElseIf defaultResult = WpfMessageBoxResult.Cancel Then
            btnCancel.IsDefault = True
            btnCancel.Focus()
            btnYes.IsDefault = False
            btnNo.IsDefault = False
            btnOk.IsDefault = False
        ElseIf defaultResult = WpfMessageBoxResult.OK Then
            btnOk.IsDefault = True
            btnOk.Focus()
            btnYes.IsDefault = False
            btnNo.IsDefault = False
            btnCancel.IsDefault = False
        End If
    End Sub

    Private Sub SetImage(ByVal image As WpfMessageBoxImage)
        If image = WpfMessageBoxImage.Error Then
            MessageImage.Source = New BitmapImage(New Uri("CustomDialogStop.png", UriKind.Relative))
        ElseIf image = WpfMessageBoxImage.Warning Then
            MessageImage.Source = New BitmapImage(New Uri("CustomDialogWarning.png", UriKind.Relative))
        ElseIf image = WpfMessageBoxImage.Question Then
            MessageImage.Source = New BitmapImage(New Uri("CustomDialogQuestion.png", UriKind.Relative))
        ElseIf image = WpfMessageBoxImage.Information Then
            MessageImage.Source = New BitmapImage(New Uri("CustomDialogInformation.png", UriKind.Relative))
        End If
    End Sub

    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnOk.Click
        mWpfMessageBoxResult = WpfMessageBoxResult.OK
        Me.DialogResult = CType(mWpfMessageBoxResult, Boolean?)

    End Sub

    Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnYes.Click
        mWpfMessageBoxResult = WpfMessageBoxResult.Yes
        Me.DialogResult = CType(mWpfMessageBoxResult, Boolean?)
    End Sub

    Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNo.Click
        mWpfMessageBoxResult = WpfMessageBoxResult.No
        Me.DialogResult = CType(mWpfMessageBoxResult, Boolean?)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        mWpfMessageBoxResult = WpfMessageBoxResult.Cancel
        Me.DialogResult = CType(mWpfMessageBoxResult, Boolean?)
    End Sub

    Private Sub WpfMessageBox_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If mWpfMessageBoxButton = WpfMessageBoxButton.YesNo Then
            If mWpfMessageBoxResult <> WpfMessageBoxResult.Yes And mWpfMessageBoxResult <> WpfMessageBoxResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub


    Private Sub WpfMessageBox_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        mWpfMessageBoxResult = WpfMessageBoxResult.None


        Dim res As New ResourceDictionary()
        If mSkin = DisplaySkin.Black Then
            res.Source = New Uri("BlackSkin.xaml", UriKind.Relative)
        ElseIf mSkin = DisplaySkin.Blue Then
            res.Source = New Uri("BlueSkin.xaml", UriKind.Relative)
        End If

        Me.Resources.Clear()
        Me.Resources = res
    End Sub


End Class


